package com.kb.erp.entity.quote;



import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.lang.reflect.Parameter;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Data
public class ResponseDeJie4 {
    private int status;
    private int count;
    private String msg;
    private List<ProductWrapper> data;


    @JsonIgnoreProperties(ignoreUnknown = true)
    @Data
    public static class ProductWrapper {
        @JsonProperty("Product")
        private Product product;
    }



    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Product {
        @JsonProperty("Description")
        private Description description;
        @JsonProperty("ManufacturerProductNumber")
        private String manufacturerProductNumber;
        @JsonProperty("UnitPrice")
        private double unitPrice;
        @JsonProperty("ProductUrl")
        private String productUrl;
        @JsonProperty("DatasheetUrl")
        private String datasheetUrl;
        @JsonProperty("PhotoUrl")
        private String photoUrl;
        @JsonProperty("ProductVariations")
        private List<ProductVariation> productVariations;
        @JsonProperty("QuantityAvailable")
        private int quantityAvailable;
        @JsonProperty("BackOrderNotAllowed")
        private boolean backOrderNotAllowed;
        @JsonProperty("NormallyStocking")
        private boolean normallyStocking;
        @JsonProperty("Discontinued")
        private boolean discontinued;
        @JsonProperty("EndOfLife")
        private boolean endOfLife;
        @JsonProperty("Ncnr")
        private boolean ncnr;
        @JsonProperty("PrimaryVideoUrl")
        private String primaryVideoUrl;
        @JsonProperty("DateLastBuyChance")
        private String dateLastBuyChance;
        @JsonProperty("ManufacturerLeadWeeks")
        private String manufacturerLeadWeeks;
        @JsonProperty("ManufacturerPublicQuantity")
        private int manufacturerPublicQuantity;
        @JsonProperty("Manufacturer")
        private Manufacturer manufacturer;


        @JsonProperty("Parameters")
        private List<Parameters> parameters;

        @JsonProperty("Classifications")
        private Classifications classifications;

    }


    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Parameters {
        @JsonProperty("ParameterId")
        private Integer parameterId;
        @JsonProperty("ValueText")
        private String valueText;
    }

    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Classifications{
        @JsonProperty("ExportControlClassNumber")
        private String ExportControlClassNumber;

    }

    @Data
    public static class Manufacturer {
        @JsonProperty("Id")
        private int id;

        @JsonProperty("Name")
        private String name;
    }

    @Data
    public static class Description {
        @JsonProperty("ProductDescription")
        private String productDescription;
        @JsonProperty("DetailedDescription")
        private String detailedDescription;
    }


    @Data
    public static  class ProductVariation {
        @JsonProperty("DigiKeyProductNumber")
        private String digiKeyProductNumber;

        @JsonProperty("PackageType")
        private PackageType packageType;

        @JsonProperty("StandardPricing")
        private List<Pricing> standardPricing;

        @JsonProperty("MyPricing")
        private List<Pricing> myPricing;

        @JsonProperty("MarketPlace")
        private boolean marketPlace;

        @JsonProperty("TariffActive")
        private boolean tariffActive;

        @JsonProperty("Supplier")
        private Supplier supplier;

        @JsonProperty("QuantityAvailableforPackageType")
        private int quantityAvailableforPackageType;

        @JsonProperty("MaxQuantityForDistribution")
        private int maxQuantityForDistribution;

        @JsonProperty("MinimumOrderQuantity")
        private int minimumOrderQuantity;

        @JsonProperty("StandardPackage")
        private int standardPackage;

        @JsonProperty("DigiReelFee")
        private int digiReelFee;


        //数量
        public String generatePriceString3() {
            StringBuilder sb = new StringBuilder();
            boolean isFirst = true;
            if (standardPricing != null && !standardPricing.isEmpty()) {
                for (Pricing standardPrice : standardPricing) {
                    if (!isFirst) {
                        sb.append("<br/>");
                    }
                    sb.append(standardPrice.getBreakQuantity())

                    ;
                    isFirst = false;
                }
            }
            return sb.toString();
        }


        //数量
        public Integer generateMinNum() {

            if (standardPricing != null && !standardPricing.isEmpty()) {
                for (Pricing standardPrice : standardPricing) {

                    return standardPrice.getBreakQuantity();



                }
            }
            return null;
        }

        //折扣价
        public String generatePriceString() {
            StringBuilder sb = new StringBuilder();
            Map<Integer, Double> myPricingMap = new HashMap<>();

            // 将 MyPricing 中的价格映射到一个 Map 中
            if (myPricing != null && !myPricing.isEmpty()) {
                for (Pricing pricing : myPricing) {
                    myPricingMap.put(pricing.getBreakQuantity(), pricing.getUnitPrice());
                }
            }

            // 遍历 StandardPricing，按照标准价输出
            boolean isFirst = true;
            if (standardPricing != null && !standardPricing.isEmpty()) {
                for (Pricing standardPrice : standardPricing) {
                    if (!isFirst) {
                        sb.append("<br/>");
                    }

                    Double myPrice = myPricingMap.get(standardPrice.getBreakQuantity());
                    if (myPrice != null) {
                        sb.append(myPrice);
                    } else {
                        sb.append(""); // 没有匹配的 MyPricing，保留空行
                    }

                    isFirst = false;
                }
                sb.append("<br/>");
            }

            return sb.toString();
        }



        //标准价
        public String generatePriceString2() {
            StringBuilder sb = new StringBuilder();
            boolean isFirst = true;
            if (standardPricing != null && !standardPricing.isEmpty()) {
                for (Pricing standardPrice : standardPricing) {
                    if (!isFirst) {
                        sb.append("<br/>");
                    }
                    sb .append(standardPrice.getUnitPrice())
                    ;
                    isFirst = false;
                }
            }

            return sb.toString();
        }



    // 计算最小公倍数（LCM）
  public static int largestMultiple(int quantity, int breakQuantity) {
        return (quantity / breakQuantity) * breakQuantity;
    }
        public  void findBestPricing(int quantity,QuoteVO tem,String currency,int query,Boolean mult) {
            if(quantity==0){
                return;
            }


            Pricing closestStandard = null;
            Pricing closestMy = null;


            // 找到不超过给定数量的最大标准定价
            for (Pricing pricing : standardPricing) {
                if (pricing.getBreakQuantity() <= quantity) {
                    if (closestStandard == null || pricing.getBreakQuantity() > closestStandard.getBreakQuantity()) {
                        closestStandard = pricing;
                    }
                }
            }

            // 找到不超过给定数量的最大折扣定价
            for (Pricing pricing : myPricing) {
                if (pricing.getBreakQuantity() <= quantity) {
                    if (closestMy == null || pricing.getBreakQuantity() > closestMy.getBreakQuantity()) {
                        closestMy = pricing;
                    }
                }
            }

            if (closestStandard != null ) {

                if(mult){
                            tem.setResult(String.valueOf(largestMultiple(quantity,standardPricing.get(0).getBreakQuantity())));
                }else{
                            tem.setResult(String.valueOf(closestStandard.getBreakQuantity()));
                }


                // 在您的代码中替换 double 为 BigDecimal
                BigDecimal unitPrice;
                if (closestMy != null) {
                    unitPrice = BigDecimal.valueOf(closestMy.getUnitPrice());
                } else {
                    unitPrice = BigDecimal.valueOf(closestStandard.getUnitPrice());
                }



// 更新 result1
                if(tem.getVenPackage().contains("Digi-Reel")){
                    // 计算额外值
                    BigDecimal additionalValue = Objects.equals(currency, "CNY")
                            ? BigDecimal.valueOf(49.0).divide(BigDecimal.valueOf(query),4, BigDecimal.ROUND_HALF_UP)
                            : BigDecimal.valueOf(7.0).divide(BigDecimal.valueOf(query),4, BigDecimal.ROUND_HALF_UP);
                    tem.setResult1(unitPrice.add(additionalValue).toString());
                }
                else{
                    tem.setResult1(unitPrice.toString());
                }
                tem.setResult2(String.valueOf( closestStandard.getUnitPrice()));




                if (closestMy == null && closestStandard != null) {
                    // 如果a取不到但b取到，返回1
                    tem.setResult4(BigDecimal.ONE.toString());
                } else if (closestMy == null || closestStandard == null) {
                    // 如果a和b都取不到，或者b取不到，返回0
                    tem.setResult4(BigDecimal.ZERO.toString());
                } else {
                    double myPrice = closestMy.getUnitPrice();  // a
                    double standardPrice = closestStandard.getUnitPrice();  // b

                    if (myPrice == 0) {
                        // 如果a为0，直接返回0
                        tem.setResult4(BigDecimal.ZERO.toString());
                    } else if (myPrice != 0 && standardPrice != 0) {
                        // 如果a和b都有值且不为0，返回a除以b的结果
                        tem.setResult4(new BigDecimal(tem.getResult1()).divide(new BigDecimal(tem.getResult2()), 2, BigDecimal.ROUND_HALF_UP).toString());
                    } else {
                        // 其他情况返回0
                        tem.setResult4(BigDecimal.ZERO.toString());
                    }
                }



            }



        }

    }

    @Data
    public static  class PackageType {
        @JsonProperty("Id")
        private int id;

        @JsonProperty("Name")
        private String name;
    }



    @Data
    public static class Pricing {
        @JsonProperty("BreakQuantity")
        private int breakQuantity;

        @JsonProperty("UnitPrice")
        private double unitPrice;

        @JsonProperty("TotalPrice")
        private double totalPrice;




    }

    @Data
    public static  class Supplier {
        @JsonProperty("Id")
        private int id;

        @JsonProperty("Name")
        private String name;
    }



}





